#include "XMLHandler.h"

XMLHandler::XMLHandler()  
{

}
XMLHandler::~XMLHandler()  
{

}

void XMLHandler::initFile() {
	/*
 	TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );  
	currentDocument.LinkEndChild( decl );  

	mRootObject = new TiXmlElement( "ApeEngine" );
	currentDocument.LinkEndChild( mRootObject );

	TiXmlComment * comment = new TiXmlComment();
	comment->SetValue("Ape Engine map file" );  
	mRootObject->LinkEndChild( comment );  
*/

}

void XMLHandler::saveFile(Ogre::String fileName) {
	
	bool addResource = true;
	//StringVectorPtr resourceNames = ResourceGroupManager::getSingleton().findResourceNames("ApeEngineMaps","",true );
	
	//Ogre::ResourceGroupManager::ResourceDeclarationList rdl = ResourceGroupManager::getSingleton().getResourceDeclarationList("ApeEngineMaps");

	//StringVectorPtr resourceNames = ResourceGroupManager::getSingleton().listResourceNames("ApeEngineMaps",true);
	//std::vector<Ogre::String>::const_iterator itResourceName = resourceNames->begin();
	//std::list<Ogre::String>::const_iterator itResourceName = rdl.begin(); // resourceNames->begin();
	//Ogre::ResourceGroupManager::ResourceDeclarationList::iterator itResourceName = rdl.begin(); 

	/*
	Ogre::ResourceGroupManager::ResourceGroup* grp = Ogre::ResourceGroupManager::getResourceGroup("ApeEngineMaps");
	Ogre::ResourceManager::ResourceMap ResourceGroupManager::getSingleton().resourceExists(resGroup, fName)
	Ogre::ResourceGroupManager::ResourceDeclarationList::iterator i = grp->resourceDeclarations.begin();
	while ( i != grp->resourceDeclarations.end() ) //rdl.end() resourceNames->end()
	{
		std::string dirName = i->resourceName;
		++i;
		addResource = false;
	}
	*/
/*
	Ogre::StringVectorPtr names = Ogre::ResourceGroupManager::getSingleton().listResourceNames("ApeEngineMaps",true);
	for(int i = 0;i<names->size();i++)
	{
		std::string dirName = (*names)[i];
	}
*/
	/*
	ConfigFile cf;
	cf.load("resources.cfg");   

	ConfigFile::SectionIterator seci = cf.getSectionIterator();   
	while (seci.hasMoreElements())
	{
	  String secName = seci.peekNextKey();
	  ConfigFile::SettingsMultiMap* settings = seci.getNext();
	  for (ConfigFile::SettingsMultiMap::iterator i = settings->begin();
		   i != settings->end(); ++i)
	  {
		String typeName = i->first;
		String archName = i->second;
		ResourceGroupManager::getSingleton().ge addResourceLocation(archName, typeName, secName);
		ResourceGroupManager::getSingleton().lio
	  }
	}
	*/


	//fileName += ".xml";
	currentDocument.SaveFile( fileName.c_str()); 
}
TiXmlElement* XMLHandler::createParentElement(Ogre::String name) {
 	TiXmlElement * element = new TiXmlElement(name.c_str());  
	mRootObject->LinkEndChild( element );  
	return element;
}
TiXmlElement* XMLHandler::createChildElement(TiXmlElement *parent,Ogre::String name) {
	TiXmlElement *child = new TiXmlElement(name.c_str());  
	parent->LinkEndChild( child );  
	return child;
}

void XMLHandler::write_OgreConf()
{  
	/*
	TiXmlElement* msg;
 	TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );  
	currentDocument.LinkEndChild( decl );  
 
	mRootObject = new TiXmlElement( "ApeEngine" );
	currentDocument.LinkEndChild( mRootObject );
	*/

	TiXmlElement* msg;
 	TiXmlElement * msgs = new TiXmlElement( "Messages" );  
	mRootObject->LinkEndChild( msgs );  
 
	msg = new TiXmlElement( "Welcome" );  
	msg->LinkEndChild( new TiXmlText( "Welcome to MyApp" ));  
	msgs->LinkEndChild( msg );  
 
	msg = new TiXmlElement( "Farewell" );  
	msg->LinkEndChild( new TiXmlText( "Thank you for using MyApp" ));  
	msgs->LinkEndChild( msg );  
 
	TiXmlElement * windows = new TiXmlElement( "Windows" );  
	mRootObject->LinkEndChild( windows );  

	TiXmlElement * window;
	window = new TiXmlElement( "Window" );  
	windows->LinkEndChild( window );  
	window->SetAttribute("name", "MainFrame");
	window->SetAttribute("x", 5);
	window->SetAttribute("y", 15);
	window->SetAttribute("w", 400);
	window->SetAttribute("h", 250);

	TiXmlElement * cxn = new TiXmlElement( "Connection" );
	mRootObject->LinkEndChild( cxn );  
	cxn->SetAttribute("ip", "192.168.0.1");
	cxn->SetDoubleAttribute("timeout", 123.456); // floating point attrib
	
	//dump_to_stdout( &doc );
	//doc.SaveFile( "appsettings.xml" );  
} 
